import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/10/18
 * Time: 9:47
 * Description:
 */
public class HomeWork {

    /**
     * 朱自一
     * 每次从i位置  确定j的范围是i  i+3
     * @param arr
     * @return
     */
    public static boolean findContiOdd(int[] arr){
        if(arr.length < 3){
            return false;
        }
        for(int i=0;i<=arr.length-3;i++){
            int count=0;
            for(int j=i;j<i+3;j++){
                if(arr[j]%2!=0){
                    count++;
                }else{
                    break;
                }
            }
            if(count==3){
                return true;
            }
        }
        return false;
    }

    public static boolean fun(int[] arr){
        for(int i = 0;i < arr.length-2;i++){
            if((arr[i] % 2 == 1) && (arr[i+1] % 2 == 1) &&(arr[i+2] % 2 == 1)) {
                System.out.println("存在连续三个元素都是奇数的情况，即["+arr[i]+","+arr[i+1]+","+arr[i+2]+"]。");
                return true;
            }
        }
        System.out.println("不存在连续三个元素都是奇数的情况。");
        return false;
    }

    public boolean threeConsecutiveOdds(int[] arr) {
        if (arr == null) return false;
        int len = arr.length;
        int consecutiveOdds = 0;//目前连续的奇数个数
        for (int i = 0; i < len; i++) {
            if ((arr[i] & 1) == 1) {
                consecutiveOdds++;
            } else {
                consecutiveOdds = 0;
            }
            if (consecutiveOdds == 3) {
                return true;
            }
        }
        return false;
    }

    public static  boolean threeConsecutiveOdds2(int []array){
        for (int i = 0; i <array.length-2 ; i++) {
            int j = 0;
            for ( j = i; j <=i+2 ; j++) {
                if (array[j]%2==0){
                    break;
                }
            }
            if (j>i+2){
                return true;
            }
        }
        return false ;
    }


    public static int findNum(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int count = 0;
            for (int j = i; j < array.length; j++) {
                if(array[i] == array[j]) {
                    count++;
                }
            }
            if(count > array.length/2) {
                return array[i];
            }
        }
        //
        return -1;//标志没有超过一半的数字
    }


    public int majorityElement(int[] nums) {
//2.参考题解思路的摩尔投票法
        int winer = nums[0];
        int people = 1;//目前主导势力的人数
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == winer) {
//如果是是自己人 势力增强
                people++;
            } else {
//如果是敌人 且目前主导势力还有士兵 则主导势力人数减少
                if (people != 0) {
                    people--;
                } else {
//如果主导势力已经没有士兵 则主导势力转移 人数增加
                    winer = nums[i];
                    people++;
                }
            }
        }
        return winer;//最后的胜者就是众数
    }


    public static int func(int [] array) {
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum = sum ^ array[i];
        }
        return sum;
    }
    
    public static int[] func2(int[] array,int target) {
        int[] nums = {-1,-1};
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] + array[j] == target) {
                    nums[0] = i;
                    nums[1] = j;
                    return nums;
                }
            }
        }
        return nums;
    }


    public static void reverse(int[] array) {
        //array = new int[10];
        int i = 0;
        int j = array.length-1;
        while (i < j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
            i++;
            j--;
        }
    }

    public static void func3(int[] array) {
        int i = 0;
        int j = array.length-1;
        while (i < j) {
            while ( i < j && array[i]%2 == 0) {
                i++;
            }
            while ( i < j && array[j]%2 != 0) {
                j--;
            }
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }


    public static int[] copy1(int[] array) {
        int[] copy = new int[array.length];//存放拷贝后的元素
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }


    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = array.clone();//克隆-》array指向的对象的副本
        System.out.println(Arrays.toString(ret));
    }

    /**
     * arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
     * src:你要拷贝的数组
     * srcPos:你要拷贝的数组,从哪里开始拷贝
     * dest:目的地。
     *
     * len:需要拷贝的长度，如果长度太大 ，那么就有可能越界
     * @param args
     */
    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(copy));
    }

    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5};
        //需要拷贝的数组    拷贝的新的长度
        int[] ret = Arrays.copyOf(array,2*array.length);
        System.out.println(Arrays.toString(ret));

        int[] ret2 = Arrays.copyOfRange(array,1,3);
        System.out.println(Arrays.toString(ret2));

    }


    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = copy1(array);
        System.out.println(Arrays.toString(ret));
    }

    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
        reverse(array);
        System.out.println(Arrays.toString(array));
        //System.out.println(func(array));
    }

    public static void main3(String[] args) {
        int[] arr={1,2,34,3,4,5,7,23,12};
        int[] arr1={2,3,4,5};
        int[] arr2={2,4,6,8};
        int[] arr3={1,1};
        if(findContiOdd(arr3)){
            System.out.println("存在三个连续奇数");
        }else{
            System.out.println("不存在三个连续奇数");
        }
    }

    public static int frogJump(int n) {
        if(n == 1) {
            return 1;
        }
        if(n == 2) {
            return 2;
        }
        return frogJump(n-1)+frogJump(n-2);
    }

    public static int frogJump2(int n) {
        if(n == 1) {
            return 1;
        }
        if(n == 2) {
            return 2;
        }
        int f1 = 1;
        int f2 = 2;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1+f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }

   public static void initArray(int[] array) {
       for (int i = 0; i < array.length; i++) {
           array[i] = i+1;
       }
   }

    public static void main1(String[] args) {
        int[] array = new int[ 100];
        initArray(array);
        System.out.println(Arrays.toString(array));

        /*System.out.println(frogJump2(1));
        System.out.println(frogJump2(2));
        System.out.println(frogJump2(3));
        System.out.println(frogJump2(4));*/



    }
}
